﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.WebControls;

namespace DataLib.Accounts
{
    public partial class TreeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                grid.BorderWidth = Unit.Pixel(1);
                grid.CellPadding = 5;
                grid.CellSpacing = 0;

                int pageIndex = 1;
                if (Request.Params["page"] != null && Request.Params["page"].ToString() != "")
                {
                    Session["Modulepagesys"] = Convert.ToInt32(Request.Params["page"]);
                    pageIndex = Convert.ToInt32(Request.Params["page"]);
                }
                else
                {
                    if (Session["Modulepagesys"] != null && Session["Modulepagesys"].ToString() != "")
                    {
                        pageIndex = Convert.ToInt32(Session["Modulepagesys"]);
                    }
                    else
                    {
                        pageIndex = 1;
                        Session["Modulepagesys"] = 1;
                    }
                }
                BiudTree();
                dataBind(pageIndex);
            }
        }
        private void BiudTree()
        {
            DataLib.BLL.SysTree sm = new DataLib.BLL.SysTree();
            DataTable dt = sm.GetTreeList("").Tables[0];

            this.listTarget.Items.Clear();
            //加载树
            this.listTarget.Items.Add(new ListItem("根目录", "0"));
            DataRow[] drs = dt.Select("ParentID= " + 0);

            foreach (DataRow r in drs)
            {
                string nodeid = r["NodeID"].ToString();
                string text = r["TreeText"].ToString();
                string parentid = r["ParentID"].ToString();
                string permissionid = r["PermissionID"].ToString();
                text = "╋" + text;
                this.listTarget.Items.Add(new ListItem(text, nodeid));
                int sonparentid = int.Parse(nodeid);
                string blank = "├";

                BindNode(sonparentid, dt, blank);

            }
            this.listTarget.DataBind();

        }
        private void BindNode(int parentid, DataTable dt, string blank)
        {
            DataRow[] drs = dt.Select("ParentID= " + parentid);

            foreach (DataRow r in drs)
            {
                string nodeid = r["NodeID"].ToString();
                string text = r["TreeText"].ToString();
                string permissionid = r["PermissionID"].ToString();
                text = blank + "『" + text + "』";

                this.listTarget.Items.Add(new ListItem(text, nodeid));
                int sonparentid = int.Parse(nodeid);
                string blank2 = blank + "─";


                BindNode(sonparentid, dt, blank2);
            }
        }

        private void dataBind(int pageIndex)
        {
            pageIndex--;
            DataLib.BLL.SysTree sm = new DataLib.BLL.SysTree();
            string strWhere = "";
            if (Session["strWheresys"] != null && Session["strWheresys"].ToString() != "")
            {
                strWhere = Session["strWheresys"].ToString();
            }
            if (listTarget.SelectedItem != null)
            {
                string nodeid = listTarget.SelectedValue;
                if (strWhere.Trim() != "")
                {
                    strWhere = strWhere + " and ";
                }
                strWhere += "ParentID= " + nodeid;
            }

            DataSet ds = new DataSet();
            ds = sm.GetTreeList(strWhere);
            grid.DataSource = ds.Tables[0].DefaultView;
            int record_Count = ds.Tables[0].Rows.Count;
            int page_Size = grid.PageSize;
            int totalPages = int.Parse(Math.Ceiling((double)record_Count / page_Size).ToString());
            if (totalPages > 0)
            {
                if ((pageIndex + 1) > totalPages)
                    pageIndex = totalPages - 1;
            }
            else
            {
                pageIndex = 0;
            }
            grid.CurrentPageIndex = pageIndex;
            grid.DataBind();
            int page_Count = grid.PageCount;
            int page_Current = pageIndex + 1;

            Page011.Record_Count = record_Count;
            Page011.Page_Count = page_Count;
            Page021.Page_Count = page_Count;

            Page011.Page_Size = page_Size;
            Page021.Page_Size = page_Size;
            Page011.Page_Current = page_Current;
            Page021.Page_Current = page_Current;
        }


        protected void grid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            //foreach (System.Web.UI.WebControls.HyperLink link in e.Item.Cells[7].Controls)
            //{
            //    link.Attributes.Add("onClick", "if (!window.confirm('您真的要删除这条记录吗？')){return false;}");
            //}
        }

        protected void listTarget_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listTarget.SelectedItem != null)
            {
                dataBind(1);
            }
 
        }


    }
}